package com.zjw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zjw.constants.SystemConstants;
import com.zjw.annotation.domain.ResponseResult;
import com.zjw.annotation.domain.entity.Link;
import com.zjw.annotation.domain.vo.AllLinkVo;
import com.zjw.annotation.domain.vo.LinkVo;
import com.zjw.annotation.domain.vo.PageVo;
import com.zjw.mapper.LinkMapper;
import com.zjw.service.LinkService;
import com.zjw.utils.BeanCopyUtils;
import io.jsonwebtoken.lang.Strings;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 友链(Link)表服务实现类
 *
 * @author makejava
 * @since 2023-12-13 16:22:13
 */
@Service("linkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    /**
     * 获取全部链接
     *
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult getAllLink() {
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Link::getStatus, SystemConstants.LINK_AUDITED);
        List<Link> list = list(wrapper);

        List<AllLinkVo> res = BeanCopyUtils.copyList(list, AllLinkVo.class);
        return ResponseResult.okResult(res);
    }

    @Override
    public ResponseResult getLinkList(Long pageNum, Long pageSize, String name, String status) {
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        if(Strings.hasText(name)){
            wrapper.like(Link::getName, name);
        }
        if(Strings.hasText(status)){
            wrapper.eq(Link::getStatus, status);
        }
//        创建分页对象
        IPage page = new Page(pageNum, pageSize);

        page(page,wrapper);
        List rawList = page.getRecords();
        List<LinkVo> res = BeanCopyUtils.copyList(rawList, LinkVo.class);

        return ResponseResult.okResult(new PageVo(res, page.getTotal()));
    }

    @Override
    public ResponseResult addLink(Link link) {
        save(link);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getLinkById(Long id) {
        Link link = getById(id);
        return ResponseResult.okResult(BeanCopyUtils.copy(link, LinkVo.class));
    }

    @Override
    public ResponseResult editLink(Link link) {
        updateById(link);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteLink(List<Long> idList) {
        idList.stream().forEach(id -> {
            removeById(id);
        });
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult audit(Long id, String status) {
        LambdaUpdateWrapper<Link> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Link::getId, id);
        wrapper.set(Link::getStatus, status);
        update(wrapper);
        return ResponseResult.okResult();
    }
}

